package com.tsingtree.gen.core.generator.ts.sqls;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.tsingtree.gen.core.generator.base.AbstractCustomGenerator;
import com.tsingtree.gen.core.util.TemplateUtil;
import org.beetl.core.Template;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author jagger
 */
public class MenuSqlGenerator extends AbstractCustomGenerator {

    public MenuSqlGenerator(Map<String, Object> tableContext) {
        super(tableContext);
    }

    @Override
    public void bindingOthers(Template template) {
        TableInfo tableInfo = (TableInfo) tableContext.get("table");
        template.binding("menus", getMenus(tableInfo));
    }

    @Override
    public String getTemplateResourcePath() {
        return "/templates/menu_sql.sql.btl";
    }

    @Override
    public String getGenerateFileTempPath() {
        String lowerEntity = (String) this.tableContext.get("lowerEntity");
        File file = new File(contextParam.getOutputPath() + "/sqls/" + lowerEntity + "_menus.sql");
        return file.getAbsolutePath();
    }

    @Override
    public String getGenerateFileDirectPath() {
        return  this.getGenerateFileTempPath();
    }

    private List<MenuModel> getMenus(TableInfo tableInfo) {
        ArrayList<MenuModel> menuModels = new ArrayList<>();

        MenuModel parentMenu = new MenuModel();
        parentMenu.setMenuId(IdWorker.getId());
        parentMenu.setCode(StrUtil.toUnderlineCase(tableInfo.getEntityName()).toUpperCase());
        parentMenu.setPcode("service");
        parentMenu.setPcodes("[0],[service],");
        if (StrUtil.isBlank(TemplateUtil.cleanWhite(tableInfo.getComment()))) {
            parentMenu.setName("空表名");
        } else {
            parentMenu.setName(TemplateUtil.cleanWhite(tableInfo.getComment()));
        }
        parentMenu.setIcon("fa-star");
        parentMenu.setUrl("/" + TemplateUtil.lowerFirst((String) tableContext.get("entity")));
        parentMenu.setSort(999);
        parentMenu.setLevels(2);
        parentMenu.setMenuFlag("Y");
        parentMenu.setSystemType("BASE_SYSTEM");
        menuModels.add(parentMenu);

        String entityName = StrUtil.toUnderlineCase(tableInfo.getEntityName()).toUpperCase();
        String parentCode = parentMenu.getCode();
        String parentName = parentMenu.getName();

        add(menuModels, entityName, parentCode, parentName, "list", "LIST", "查看");
        add(menuModels, entityName, parentCode, parentName, "addItem", "ADD", "添加");
        add(menuModels, entityName, parentCode, parentName, "editItem", "EDIT", "修改");
        add(menuModels, entityName, parentCode, parentName, "delete", "DELETE", "删除");

        return menuModels;

    }

    private void add(ArrayList<MenuModel> menuModels, String entityName, String parentCode, String parentName, String type, String typeStr, String str){
        MenuModel menu = new MenuModel();
        menu.setMenuId(IdWorker.getId());
        menu.setCode(entityName + "_" + typeStr);
        menu.setPcode(parentCode);
        menu.setPcodes("[0],[service],[" + parentCode + "],");
        menu.setName(parentName + str);
        menu.setIcon("fa-star");
        menu.setSort(999);
        menu.setUrl("/" + TemplateUtil.lowerFirst((String) tableContext.get("entity")) + "/" + type);
        menu.setLevels(3);
        menu.setMenuFlag("N");
        menu.setSystemType("BASE_SYSTEM");
        menuModels.add(menu);
    }
}
